package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.config.skin.SkinInfo;
import com.yanqu.road.entity.config.skin.SkinPatronsSkill;
import com.yanqu.road.entity.enums.eSkinType;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author wpj
 * @Date 2020/9/1
 */
public class SkinConfigDaoImpl extends TempDao {
    public static String getSkinInfoSql = "select * from t_p_skins";
    public Map<Integer, SkinInfo> getSkinInfoMap() throws Exception {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, SkinInfo> skinInfoMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getSkinInfoSql);
            rs = call.executeQuery();
            while (rs.next()) {
                SkinInfo skinInfo = new SkinInfo();
                skinInfo.setSkinId(rs.getInt("skinId"));
                skinInfo.setType(rs.getInt("type"));
                skinInfo.setQuality(rs.getInt("quality"));
                skinInfo.setBelongRole(rs.getInt("belongRole"));
                skinInfo.setSkinEffect(StringUtils.stringToIntegerList(rs.getString("skinEffect"), ";"));
                skinInfo.setCombId(rs.getInt("combId"));
                skinInfo.setIcon(rs.getString("icon"));
                skinInfo.setSound(rs.getString("sound"));
                skinInfo.setAppearanceId(rs.getString("appearanceId"));
                skinInfo.setTag(rs.getString("tag"));
                String skinFragment = rs.getString("skinFragment");
                String[] split = skinFragment.split("\\|");
                skinInfo.setUnlockGet(Integer.parseInt(split[0]));
                skinInfo.setUpgradeGet(Integer.parseInt(split[1]));
                String linkage = rs.getString("linkage");
                if ("0".equals(linkage)|| StringUtils.isNullOrEmpty(linkage)) {
                    skinInfo.setLinkAge(0);
                }else {
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd");
                    Date date = sdf.parse(linkage);
                    skinInfo.setLinkAge(date.getTime());
                }
                skinInfo.setSkillsParam(rs.getString("skillsParam"));
                skinInfo.setTwinsId(rs.getInt("twinsId"));
                skinInfo.setPeerless(rs.getInt("isPeerless"));
                skinInfo.setBelongSameRole(rs.getInt("belongSameRole"));
                if(skinInfo.getType() == eSkinType.Patrons.getValue()){
                    List<SkinPatronsSkill> skinPatronsSkillList = new ArrayList<>();
                    if(!"0".equals(skinInfo.getSkillsParam())) {
                        String[] skinSkillList = skinInfo.getSkillsParam().split("\\|");
                        for (String skinSkill : skinSkillList) {
                            if (!StringUtils.isNullOrEmpty(skinSkill)) {
                                String[] params = skinSkill.split(";");
                                SkinPatronsSkill skinPatronsSkill = new SkinPatronsSkill();
                                skinPatronsSkill.setSkinId(skinInfo.getSkinId());
                                skinPatronsSkill.setSkillId(Integer.parseInt(params[0]));
                                skinPatronsSkill.setSkillUpgradeLimitType(Integer.parseInt(params[1]));
                                skinPatronsSkillList.add(skinPatronsSkill);
                            }
                        }
                        skinInfo.setSkinPatronsSkillList(skinPatronsSkillList);
                    }
                }else if(skinInfo.getType() == eSkinType.Beauty.getValue()){
                    if(!"0".equals(skinInfo.getSkillsParam())) {
                        skinInfo.setSkinBeautySkillUpgradeTypeList(StringUtils.stringToIntegerList(skinInfo.getSkillsParam(), ";"));
                    }
                }
                skinInfo.setIsSpecialExc(rs.getInt("isSpecialExc"));

                skinInfoMap.put(skinInfo.getSkinId(), skinInfo);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
            throw e;
        } finally {
            getDb().closeConnection(call, rs);
        }
        return skinInfoMap;
    }
}
